package com.yoi.controller;

import com.yoi.Repository.UserRepository;
import com.yoi.SendAndRecv.*;
import com.yoi.bean.Ordermessage;
import com.yoi.bean.User;
import com.yoi.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController//@RestController=@Controller_@ResponseBody
public class UserController {

    private final UserRepository userRepository;
    @Autowired
    private Sender1 sender1;
    @Autowired
    private Sender2 sender2;
    @Autowired
    BookServiceImpl bookService;
    @Autowired
    private OrderSender orderSender;
    @Autowired
    private OrderSender2 orderSender2;
    @Autowired
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @RequestMapping("/TestFirstMQ")
    public void helloFirst() throws Exception {
        sender1.send1();
        sender1.send2();
    }

    @RequestMapping("/TestSecondMQ")
    public void helloSecond() throws Exception {
        sender2.send1();
        sender2.send2();
    }

    @RequestMapping("/TestFanoutMQ")
    public void Fanout() throws Exception {
        sender2.send3();
    }

    @RequestMapping("/TestDirectMQ")
    public void Direct() throws Exception {
        sender2.send4();
    }

    //RequestMapping请求为客户端发送 PostMapping为服务端接收
/*    @RequestMapping("/person/save")
    public User save(@RequestParam String name){
        User user=new User();
        user.setName(name);
        if(userRepository.save(user)){
            System.out.println("用户对象保存成功！"+user);
        };
        return user;
    }*/

/*
    @RequestMapping(value="/hello")
    public String hello(){
        return "Hello World";
    }
*/
    //
    @RequestMapping(value="/Send")
    public void Send(){
        Ordermessage order=new Ordermessage();
        order.setId(12);
        order.setOrdername("优也钢材");
        order.setMessageid(System.currentTimeMillis()+"$"+ UUID.randomUUID().toString());
        orderSender.send(order);
    }

    @RequestMapping(value="/SendBook1")
    public String SendBook(Integer id){
        orderSender.send(bookService.getBook(id));
        return "OK";
    }

    @RequestMapping(value="/SendBook2")
    public String SendBook2(Integer id){
        orderSender2.send2(bookService.getBook(id));
        return "OK";
    }

    @RequestMapping("/index")
    public String goindex(ModelAndView modelAndView){
        return "index";
    }

    @RequestMapping("/getBooks")
    @ResponseBody
    public Map<String,Object> goBooks(Integer id){
        Map<String,Object> map=new HashMap<>();
        map.put("books",bookService.getBook(id));
        return map;
    }

    @RequestMapping("/getAllBook")
    @ResponseBody
    public Map<String,Object> AllBooks(){
        Map<String,Object> map=new HashMap<>();
        map.put("books",bookService.AllBook());
        return map;
    }

    @RequestMapping("/SendBookJosnMessage")
    @ResponseBody
    public void SendBookJosnMessage(Integer id){
        orderSender2.sendJsonMessage(bookService.getBook(id).toString());
    }

}
